/******************************************************************************
Paper: The Impact of Welfare on Intergroup Relations
Author: Akshay Dixit

CPHS: This .do file produces the following main results: 
	- Figure 2 (Income from government transfers)
	- Figure 3 (Dynamic Treatment Effects: Effect of RBS on Borrowing from Relatives or Friends)
	- Table 1 (Effect of RBS on Borrowing from Relatives or Friends)

It also produces the following supplementary results:
	- Figure S8 (Fraction borrowing from relatives or friends)
	- Table S1 (Effect of RBS on borrowings from relatives/friends for consumption)
	- Table S2 (Effect of RBS on borrowings from relatives/friends by caste category)
	- Table S14 (Effect of RBS on borrowings from sources other than relatives/friends)
	- Table S21 (Dynamic treatment effects: Effect of RBS on borrowing from relatives or friends)
	- Table S22 (Income from government transfers)
	- Table S45 (Effect of RBS on borrowings from relatives/friends (continuous treatment variable)) 
	- Table S47 (Effect of RBS on borrowing for consumption and investment)
	
******************************************************************************/

clear all
set scheme white_tableau

gl data_cmie "$identity/data/cphs"
cd "$analysis"
	
******************************************************************************

*** Effect of Rythu Bandhu on income from govt transfers ***

u "$data_cmie/merged_data_clean.dta", clear

keep if telangana == 1
keep if farmer == 1 | laborer == 1

bysort state: tab farmer	
	/*	1624 farmers, 392 laborers in Telangana	*/

* Keep relevant variables
keep hh_id state caste caste_category district district_id farmer* laborer total_income_* income_household_* income_all_members_wages_*

* Rename variables to remove remaining "of" terms and ensure that the wave is properly stored during reshape
rename *_of_* *_*

* Reshape data to long format (household-wave)
reshape long total_income_ income_household_all_sou_ income_all_members_wages_ income_household_rent_ income_household_self_pr_ income_household_private_ income_household_governm_ income_household_busines_ income_household_sale_ income_household_gamblin_, i(hh_id) j(wave) string
ren *_ *

g year = substr(wave, -2, 2)
g month = substr(wave, 1, 3)

replace year = "2017" if year == "17"
replace year = "2018" if year == "18"
replace year = "2019" if year == "19"

replace month = "01" if month == "jan"
replace month = "02" if month == "feb"
replace month = "03" if month == "mar"
replace month = "04" if month == "apr"
replace month = "05" if month == "may"
replace month = "06" if month == "jun"
replace month = "07" if month == "jul"
replace month = "08" if month == "aug"
replace month = "09" if month == "sep"
replace month = "10" if month == "oct"
replace month = "11" if month == "nov"
replace month = "12" if month == "dec"

destring year month, replace
encode wave, gen(wave_num)

drop if year == 2017 & month < 5	// Drop Jan-Apr 2017
drop if year == 2019 & month > 2	// Drop Mar-Dec 2019

* Independent variables
g post = (year == 2018 & month > 4) | (year == 2019)
g post_farmer = post * farmer

g intermediate = (caste_category == "Intermediate Caste")
g post_farmer_intermediate = post * farmer * intermediate
g post_intermediate = post * intermediate
g farmer_intermediate = farmer * intermediate

* Dependent variables
summ total_income if (year == 2018 & month <= 4) | (year == 2017)
g adj_total_income = total_income / r(mean)
summ income_household_governm if (year == 2018 & month <= 4) | (year == 2017)
g adj_income_household_governm = income_household_governm / r(mean)
summ income_household_private if (year == 2018 & month <= 4) | (year == 2017)
g adj_income_household_private = income_household_private / r(mean)

* Define district * wave for clustering standard errors
g district_wave = (district_id * 100) + wave_num
codebook district_wave

* Label variables
lab var income_household_governm "Income from govt transfers"
lab var adj_income_household_governm "Income from govt transfers (fraction of pre-period)"
lab var total_income "Total income"
lab var adj_total_income "Total income (fraction of pre-period)"
lab var post "Post"
lab var post_farmer "Post * Farmer"

* Dynamic specification with Apr 18 (month before transfers) as the base period
levelsof wave, local(levels)
foreach l of local levels {
	g `l' = (wave == "`l'")
	g farmer_`l' = farmer * `l'
	lab var farmer_`l' "Farmer * `l'"
}

gl month_farmer = "farmer_may17 farmer_jun17 farmer_jul17 farmer_aug17 farmer_sep17 farmer_oct17 farmer_nov17 farmer_dec17 farmer_jan18 farmer_feb18 farmer_mar18 farmer_may18 farmer_jun18 farmer_jul18 farmer_aug18 farmer_sep18 farmer_oct18 farmer_nov18 farmer_dec18 farmer_jan19 farmer_feb19"

areg income_household_governm $month_farmer i.month, absorb(hh_id) vce(cluster district_wave)
eststo e_income_household_governm
coefplot e_income_household_governm, keep(farmer_*) rename(farmer_*="") xlabel(, angle(45)) vert xline(12) yline(0) ytitle("Estimated coefficient")
graph export "rb_transfers_trend.pdf", as(pdf) replace

outreg2 using "rb_transfers_trend.tex", keep(farmer_*) append label tex nor2 nocons
cap erase "rb_transfers_trend.txt"

******************************************************************************

*** Effect of Rythu Bandhu on borrowings ***

u "$data_cmie/merged_data_clean.dta", clear

keep if farmer == 1 | laborer == 1
g treated = (farmer == 1)
keep if telangana == 1

* Raw trend in borrowing from relatives/friends
profileplot borrowed_rel_friends_11 borrowed_rel_friends_12 borrowed_rel_friends_13 borrowed_rel_friends_14 borrowed_rel_friends_15 borrowed_rel_friends_16, by(treated) xtitle("Wave") ytitle("Borrowed from relatives/friends") ylabel(0(0.1)1) xlabel(1 "may_aug17" 2 "sep_dec17" 3 "jan_apr18" 4 "may_aug18" 5 "sep_dec18" 6 "jan_apr19") xline(4)
graph export "borrowings_raw_trend.png", as(png) replace

* For continuous treatment variable: Income from government transfers (pre- and post-RBS)
egen govt_transfers_pre = rowtotal(income_household_governm_may17 income_household_governm_jun17 income_household_governm_jul17 income_household_governm_aug17 income_household_governm_sep17 income_household_governm_oct17 income_household_governm_nov17 income_household_governm_dec17 income_household_governm_jan18 income_household_governm_feb18 income_household_governm_mar18 income_household_governm_apr18), missing
tab farmer, summ(govt_transfers_pre)

egen govt_transfers_post = rowtotal(income_household_governm_may18 income_household_governm_jun18 income_household_governm_jul18 income_household_governm_aug18 income_household_governm_sep18 income_household_governm_oct18 income_household_governm_nov18 income_household_governm_dec18 income_household_governm_jan19 income_household_governm_feb19 income_household_governm_mar19 income_household_governm_apr19), missing
tab farmer, summ(govt_transfers_post)

* Reshape data to long format (household-wave)
foreach var in borrowed_consumption_expendi_15 borrowed_consumption_expendi_16 borrowed_consumer_durables_16 borrowed_housing_16 borrowed_education_15 borrowed_education_16 borrowed_medical_expenditure_15 borrowed_medical_expenditure_16 borrowed_wedding_15 borrowed_wedding_16 borrowed_business_15 borrowed_business_16 borrowed_investments_15 borrowed_debt_repayment_15 borrowed_debt_repayment_16 borrowed_vehicles_16 {

	replace `var' = "" if `var' == "NA"
	replace `var' = "" if `var' == "DK"
	destring `var', replace

}

keep hh_id state district region_type district_id farmer laborer has_outstanding_borrowing_* borrowed_rel_friends_* borrowed_money_lender_* borrowed_bank_* borrowed_employer_* borrowed_nbfc_dealer_* borrowed_shg_* borrowed_mfi_* borrowed_chitfunds_* borrowed_shops_* borrowed_consumption_expendi_* borrowed_consumer_durables_* borrowed_housing_* borrowed_education_* borrowed_medical_expenditure_* borrowed_wedding_* borrowed_business_* borrowed_investments_* borrowed_debt_repayment_* borrowed_vehicles_* govt_transfers* caste*

reshape long has_outstanding_borrowing_ borrowed_rel_friends_ borrowed_rel_friends_co_ borrowed_rel_friends_in_ borrowed_money_lender_ borrowed_bank_ borrowed_bank_consumpti_ borrowed_bank_investmen_ borrowed_money_lender_c_ borrowed_consumption_expendi_ borrowed_consumer_durables_ borrowed_housing_ borrowed_education_ borrowed_medical_expenditure_ borrowed_wedding_ borrowed_business_ borrowed_investments_ borrowed_debt_repayment_ borrowed_vehicles_ borrowed_employer_ borrowed_nbfc_dealer_ borrowed_shg_ borrowed_mfi_ borrowed_chitfunds_ borrowed_shops_ borrowed_shops_consumpt_ borrowed_shops_investme_, i(hh_id) j(wave)
ren *_ *

keep if wave >= 11 & wave <= 16

* Independent variables
g post = (wave >= 14)
g post_farmer = post * farmer

g govt_transfers = . 
replace govt_transfers = govt_transfers_pre if post == 0
replace govt_transfers = govt_transfers_post if post == 1

g govt_transfers_farmer = govt_transfers * farmer

	// For het. effects by caste category
g lowcaste = (caste_category == "SC" | caste_category == "ST")
g post_farmer_lowcaste = post * farmer * lowcaste
g post_lowcaste = post * lowcaste
g farmer_lowcaste = farmer * lowcaste

* Dependent variables, divided by the pre-RBS mean
foreach var in has_outstanding_borrowing borrowed_money_lender borrowed_rel_friends borrowed_housing borrowed_education borrowed_medical_expenditure borrowed_wedding borrowed_consumption_expendi borrowed_consumer_durables borrowed_business borrowed_investments borrowed_debt_repayment borrowed_vehicles borrowed_rel_friends_co borrowed_money_lender_c {
	
	summ `var' if (wave == 11 | wave == 12 | wave == 13)
	g adj_`var' = `var' / r(mean)

}

* District * wave
g district_wave = (district_id * 100) + wave
codebook district_wave

* Label variables
lab var borrowed_rel_friends "Borrowing from relatives/friends"
lab var adj_borrowed_rel_friends "Borrowing from relatives/friends (fraction of pre-period)"
lab var has_outstanding_borrowing "HH has outstanding borrowing"
lab var adj_has_outstanding_borrowing "HH has outstanding borrowing (fraction of pre-period)"
lab var borrowed_money_lender "Borrowing from money lender"
lab var adj_borrowed_money_lender "Borrowing from money lender (fraction of pre-period)"
lab var borrowed_bank "Bank"
lab var borrowed_bank_consumpti "Bank - consumption"
lab var borrowed_bank_investmen "Bank - investment"
lab var borrowed_employer "Employer"
lab var borrowed_nbfc_dealer "NBFC"
lab var borrowed_shg "SHG"
lab var borrowed_mfi "MFI"
lab var borrowed_chitfunds "Chit fund"
lab var borrowed_shops "Shops"
lab var borrowed_shops_consumpt "Shops - consumption"
lab var borrowed_shops_investme "Shops - investment"
lab var borrowed_housing "Housing"
lab var adj_borrowed_housing "Housing (fraction of pre-period)"
lab var borrowed_education "Education"
lab var adj_borrowed_education "Education (fraction of pre-period)"
lab var borrowed_medical_expenditure "Medical expenses"
lab var adj_borrowed_medical_expenditure "Medical expenses (fraction of pre-period)"
lab var borrowed_wedding "Wedding"
lab var adj_borrowed_wedding "Wedding (fraction of pre-period)"
lab var borrowed_consumption_expendi "Borrowing for consumption"
lab var adj_borrowed_consumption_expendi "Consumption (fraction of pre-period)"
lab var borrowed_consumer_durables "Consumer durables"
lab var adj_borrowed_consumer_durables "Consumer durables (fraction of pre-period)"
lab var borrowed_business "Borrowing for Business"
lab var adj_borrowed_business "Business (fraction of pre-period)"
lab var borrowed_investments "Borrowing for investment"
lab var adj_borrowed_investments "Investments (fraction of pre-period)"
lab var borrowed_debt_repayment "Debt repayment"
lab var adj_borrowed_debt_repayment "Debt repayment (fraction of pre-period)"
lab var borrowed_vehicles "Vehicles"
lab var adj_borrowed_vehicles "Vehicles (fraction of pre-period)"
lab var borrowed_rel_friends_co "Borrowing from relatives/friends for consumption"
lab var adj_borrowed_rel_friends_co "Borrowing from relatives/friends for consumption (fraction of pre-period)"

lab var post "Post"
lab var post_farmer "Post * Farmer"
lab var post_lowcaste "Post * SC/ST"
lab var post_farmer_lowcaste "Post * Farmer * SC/ST"
lab var govt_transfers "Govt transfers"
lab var govt_transfers_farmer "Govt transfers * Farmer"


* Primary diff-in-diff specification
cap erase "rb_borrowings_impact_districtwave.tex"

local outcomes borrowed_rel_friends adj_borrowed_rel_friends borrowed_rel_friends_co adj_borrowed_rel_friends_co
foreach var of local outcomes {

	areg `var' post_farmer post farmer i.wave, absorb(hh_id) vce(cluster district_wave)
	outreg2 using "rb_borrowings_impact_districtwave.tex", keep(post_farmer post farmer) append label tex nor2 nocons

}

cap erase "rb_borrowings_impact_districtwave.txt"


* Dynamic specification to evaluate pre-trends, with wave 13 (Jan-Apr 2018) as base wave
levelsof wave, local(levels)
foreach l of local levels {
	g wave_`l' = (wave == `l')
	g farmer_wave_`l' = farmer * wave_`l'
	lab var farmer_wave_`l' "Farmer * Wave `l'"
	drop wave_`l'
}

ren farmer_wave_11 farmer_may_aug2017
ren farmer_wave_12 farmer_sep_dec2017
ren farmer_wave_13 farmer_jan_apr2018
ren farmer_wave_14 farmer_may_aug2018
ren farmer_wave_15 farmer_sep_dec2018
ren farmer_wave_16 farmer_jan_apr2019

gl wave_farmer = "farmer_may_aug2017 farmer_sep_dec2017 farmer_may_aug2018 farmer_sep_dec2018 farmer_jan_apr2019"

areg borrowed_rel_friends $wave_farmer i.wave farmer, absorb(hh_id) vce(cluster district_wave)
eststo e_rel_friends
coefplot e_rel_friends, keep(farmer_*) rename(farmer_*="") xlabel(, angle(45)) vert xline(3) yline(0) ylabel(-0.4(0.1)0.4) ytitle("Estimated coefficient")
graph export "rb_borrowings_trend.pdf", as(pdf) replace

outreg2 using "rb_borrowings_trend.tex", keep(farmer_*) append label tex nor2 nocons
cap erase "rb_borrowings_trend.txt"

* Het. effects by caste category 
cap erase "rb_het_caste.tex"

local outcomes borrowed_rel_friends adj_borrowed_rel_friends
foreach var of local outcomes {
	areg `var' post_farmer_lowcaste post_farmer post_lowcaste farmer_lowcaste post farmer lowcaste i.wave, absorb(hh_id) vce(cluster district_wave)
	outreg2 using "rb_het_caste.tex", keep(post_farmer_lowcaste post_farmer post_lowcaste farmer_lowcaste post farmer lowcaste) append label tex nor2 nocons
}

cap erase "rb_het_caste.txt"

* Continuous treatment variable (amount of govt transfers)
replace govt_transfers = govt_transfers/1000
replace govt_transfers_farmer = govt_transfers_farmer/1000

cap erase "rb_borrowings_intensity.tex"

local outcomes borrowed_rel_friends adj_borrowed_rel_friends borrowed_rel_friends_co adj_borrowed_rel_friends_co
foreach var of local outcomes {
	areg `var' govt_transfers_farmer govt_transfers farmer i.wave, absorb(hh_id) vce(cluster district_wave)
	outreg2 using "rb_borrowings_intensity.tex", keep(govt_transfers_farmer govt_transfers farmer) append label tex nor2 nocons
}

cap erase "rb_borrowings_intensity.txt"

******************************************************************************

*** Effect of RBS on borrowing from other sources ***

cap erase "rb_borrowings_impact_bysource.tex"

local outcomes borrowed_money_lender borrowed_shops borrowed_employer borrowed_shg borrowed_bank borrowed_nbfc_dealer borrowed_chitfunds
foreach var of local outcomes {
	areg `var' post_farmer post farmer i.wave, absorb(hh_id) vce(cluster district_wave)
	outreg2 using "rb_borrowings_impact_bysource.tex", nocons nor2 keep(post_farmer post farmer) append label tex
}

cap erase "rb_borrowings_impact_bysource.txt"

******************************************************************************

*** Effect of RBS on borrowing by purpose ***

cap erase "rb_borrowings_impact_bypurpose.tex"

local outcomes borrowed_consumption_expendi borrowed_investments borrowed_bank_consumpti borrowed_bank_investmen borrowed_shops_consumpt borrowed_shops_investme
foreach var of local outcomes {
	dis as error "`var'"
	areg `var' post_farmer post farmer i.wave, absorb(hh_id) vce(cluster district_wave)
	outreg2 using "rb_borrowings_impact_bypurpose.tex", nocons nor2 keep(post_farmer post farmer) append label tex
}

cap erase "rb_borrowings_impact_bypurpose.txt"

******************************************************************************

clear
